import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String s=",1_1,1_11,1_111,";
		for(String i:s.split(",",3))
			System.out.println(">> "+i);
		File js_img = new File("src/main/webapp/resources/heatmap/"+2013);
		System.out.println(js_img.getAbsolutePath());
		String div="<div style='name' id=\"header\" bagcolor=''>";
		String colorBar="<div align=\"center\">\n";
		System.out.println(div.replaceFirst("<div\\s{1,}(.*?)id=('|\")header\\2(.*?)>", "<div $1id=\"header\"$3>"+colorBar));
		
//		String html=readFile(new File("bj.html"));
//		regTest(html);
	}

	public static void regTest(String html){
		Pattern p = Pattern.compile("<a(.*?)\\s*>", Pattern.DOTALL);
		Pattern href=Pattern.compile("href=(['\"])(.*?)\\1");
		Matcher m = p.matcher(html);
		StringBuffer sb = new StringBuffer();
		while (m.find()) {
			String a = m.group(1);
			Matcher mat=href.matcher(a);
			mat.find();
			String toURL=mat.group(2);
//			System.out.println(toURL);
			int pvCount=0,uvCount=0;
			String replaced = "<a style=\"background-color:+color>";
			m.appendReplacement(sb, changeColor(a,"red"));
			System.out.println(sb);
		}
		m.appendTail(sb);
		System.out.println(sb);
	}
	
	public static String changeColor(String a, String color){
		String replaced;
		int i = a.indexOf("style=\"");
		if (i > 0) {
			replaced = "<a" + a.substring(0, i)
					+ "style=\"background-color:"+color+"; " + a.substring(i + 7)
					+ ">";
		} else
			replaced = "<a" + a + " style=\"background-color:"+color+"\">";
//		System.out.println(replaced);
		return replaced;
	}
	
	
	public void format(){

//		//日期格式化
//		Calendar date=Calendar.getInstance();// TODO Auto-generated method stub
//		int year=date.get(Calendar.YEAR);
//		int month=date.get(Calendar.MONTH);
//		int day=date.get(Calendar.DAY_OF_MONTH);
//		System.out.println(new SimpleDateFormat("yyyyMMdd").format(new Date()));
//		Calendar calendar=Calendar.getInstance();
//		calendar.add(Calendar.DATE, -1);
//		System.out.println(calendar.getTime());
//		//路径测试
//		try {
//			System.out.println(new File(".","").getAbsolutePath());
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
////		Test t=new Test();
////		t.getPath();
	}
	public void getPath(){
		String configPath = System.getProperty("user.dir");
		System.out.println("user.dir"+configPath);
		System.out.println("getClass().getResource(/)"+Test.class.getResource("/").getPath());
		System.out.println("getClass().getResource()"+this.getClass().getResource("").getPath());
		System.out.println("getClass().getClassLoader().getResource(.)"+this.getClass().getClassLoader().getResource(".").getPath());
//		System.out.println("getClass().getClassLoader().getResource()"+this.getClass().getClassLoader().getResource("").getPath());
		System.out.println("Thread.currentThread().getContextClassLoader().getResource()"+Thread.currentThread().getContextClassLoader().getResource("").getPath());
		System.out.println("Thread.currentThread().getContextClassLoader().getResource(.)"+Thread.currentThread().getContextClassLoader().getResource(".").getPath());
	}
	
	public static String readFile(File file){
		String line_separator=System.getProperty("line.separator");
		StringBuilder html=new StringBuilder();
		try{
			BufferedReader br = new BufferedReader(new FileReader(file));
			String line=null;
			while((line=br.readLine())!=null){
				html.append(line);
				html.append(line_separator);
			}
			br.close();
		}catch(IOException e){
		}
		return html.toString();
	}
	
	/**
	 * 写文件
	 * @param content
	 * @param file
	 */
	public static void saveFile(String content,File file){
		try{
		BufferedWriter bw = new BufferedWriter(new FileWriter(file));
		bw.write(content);
		bw.flush();
		bw.close();
		}catch(IOException e){
		}
	}
	
}
